package com.jason.platform.supervision.activity;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.base.EventBusParameter;
import com.jason.platform.supervision.beans.LeaveSignBean;
import com.jason.platform.supervision.beans.LeaveSignResp;
import com.jason.platform.supervision.beans.UserInfoBean;
import com.jason.platform.supervision.task.LeaveApprovalTask;
import com.jason.platform.supervision.task.QueryLeaveApprovalDetailTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.utils.DateUtils;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.LoadingDialog;
import com.jason.platform.supervision.widght.TitleBar;
import de.greenrobot.event.EventBus;

/**
 * @ClassName: LeaveApprovalDetailActivity
 * @Description: 请假审批详情
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015年11月30日14:57:50
 */
public class LeaveApprovalDetailActivity extends BaseActivity implements View.OnClickListener,TaskListener {

    private static final String TAG = LeaveApprovalDetailActivity.class.getSimpleName();

    public static final String KEY_LEAVE_SIGN = "key_leave_sign";

    private TitleBar mTitleBar;

    private MyHandler<LeaveApprovalDetailActivity> mHandler = new MyHandler<LeaveApprovalDetailActivity>(this);

    private TextView
            mNameTextView,//姓名--leave_sign_detail_name
            mBeginTextView,//开始时间--leave_sign_detail_begin
            mEndTextView,//结束时间--leave_sign_detail_end
            mDaysTextView,//请假天数--leave_sign_detail_days
            mOrangeTextView,//申报司法说--leave_sign_detail_organ
            mLocationTextView,//目的地及范围--leave_sign_detail_location
            mReasonTextView,//具体事由--leave_sign_detail_reason
            mApprovalDateTextView,//审批时间--leave_sign_detail_approval_date
            mExplainTextView;//理由说明--leave_sign_detail_explain;

    private Button mOkButton,mCancelButton;

    private LeaveSignBean mLeaveSignBean;
    private String mLeaveSignId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_leave_approval_detail);
        super.onCreate(savedInstanceState);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mNameTextView = (TextView) findViewById(R.id.leave_sign_detail_name);
        mBeginTextView = (TextView) findViewById(R.id.leave_sign_detail_begin);
        mEndTextView = (TextView) findViewById(R.id.leave_sign_detail_end);
        mDaysTextView = (TextView) findViewById(R.id.leave_sign_detail_days);
        mReasonTextView = (TextView) findViewById(R.id.leave_sign_detail_reason);
        mOrangeTextView = (TextView) findViewById(R.id.leave_sign_detail_organ);
        mLocationTextView = (TextView) findViewById(R.id.leave_sign_detail_location);
        mOrangeTextView = (TextView) findViewById(R.id.leave_sign_detail_organ);
        mApprovalDateTextView = (TextView) findViewById(R.id.leave_sign_detail_approval_date);
        mExplainTextView = (TextView) findViewById(R.id.leave_sign_detail_explain);
        mOkButton = (Button) findViewById(R.id.leave_sign_detail_ok_btn);
        mCancelButton = (Button) findViewById(R.id.leave_sign_detail_cancel_btn);
        mOkButton.setOnClickListener(this);
        mCancelButton.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mLeaveSignBean = (LeaveSignBean) getIntent().getSerializableExtra(KEY_LEAVE_SIGN);
        if (mLeaveSignBean != null){
            mLeaveSignId = String.valueOf(mLeaveSignBean.getId());
        }
        updateData();
        QueryLeaveApprovalDetailTask task = new QueryLeaveApprovalDetailTask(this);
        task.type = Constant.Manager.GET_LEAVE_APPROVAL_DETAIL;
        task.mParams.put("id", mLeaveSignId);
        task.execute();
    }

    @Override
    protected void initLoadingDialog() {
        super.initLoadingDialog();
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setIsOut(false);
        mLoadingDialog.setMessage("审核中...");
    }

    private void updateData(){
        if (mLeaveSignBean == null){
            return;
        }
        mNameTextView.setText(mLeaveSignBean.getName());
        mBeginTextView.setText(mLeaveSignBean.getStartTime());
        mEndTextView.setText(mLeaveSignBean.getEndTime());
        mDaysTextView.setText(mLeaveSignBean.getLeaveDays());

        mReasonTextView.setText(mLeaveSignBean.getLeavecontent());
        mOrangeTextView.setText(mLeaveSignBean.getJudicialplace());
        mLocationTextView.setText(mLeaveSignBean.getLeavedestinationandarea());
        mApprovalDateTextView.setText(DateUtils.converDatLongToString(System.currentTimeMillis(),DateUtils.DATE_YMD_HMS));//当前时间
        mExplainTextView.setText("");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.leave_sign_detail_ok_btn){
            //TODO 同意
            leaveApproval(1);
        } else if (v.getId() == R.id.leave_sign_detail_cancel_btn) {
            //TODO 不同意
            leaveApproval(0);
        }
    }


    private void leaveApproval(int flag){
        mLoadingDialog.show();
        String content = mExplainTextView.getText().toString();
        String time = DateUtils.converDatLongToString(System.currentTimeMillis(),DateUtils.DATE_YMD_HMS);
        UserInfoBean bean = SharedPreferencesUtils.getUserInfoBean(this);
        LeaveApprovalTask task = new LeaveApprovalTask(this);
        task.type = Constant.Manager.GET_LEAVE_APPROVAL;
        task.mParams.put("id", mLeaveSignId);
        task.mParams.put("content", content);
        task.mParams.put("organId", String.valueOf(SharedPreferencesUtils.getOrganId(this)));
        task.mParams.put("organ", bean == null ? "" : bean.getOrgan());
        task.mParams.put("flag", String.valueOf(flag));
        task.mParams.put("time", time);
        task.execute();
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.Manager.GET_LEAVE_APPROVAL_DETAIL) {
            if (task.isSuccess) {
                LeaveSignResp resp = (LeaveSignResp) task.resData;
                mLeaveSignBean = resp.getValues().get(0);
                updateData();
            }
        }else if (task.type == Constant.Manager.GET_LEAVE_APPROVAL){
            if (mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess){
//                finish();
                //TODO 更新页面
                EventBus.getDefault().post(new EventBusParameter(EventBusParameter.ACTION_UPDATE_LEAVE_APPROVAL_LIST,mLeaveSignId));
            }
            Toast.makeText(this,task.rspDesc,Toast.LENGTH_SHORT).show();
        }
    }
}
